const fs = require("fs")
const path = require("path")

// 写入文件
fs.writeFile(path.resolve(__dirname, "a.txt"), "hello world", (err) => {
    if (err) {
        console.log(err)
    } else {
        console.log("写入成功")
    }
})

// 同步写法
try {
    fs.writeFileSync(path.resolve(__dirname, "b.txt"), "hello world")
    console.log("写入成功")
} catch (error) {
    console.log(error)
}


// 追加写入
fs.appendFile(path.resolve(__dirname, "a.txt"), "你好世界", (err) => {
    if (err) {
        console.log(err)
    } else {
        console.log("追加写入成功")
    }
})

// 同步追加写入
try {
    fs.appendFileSync(path.resolve(__dirname, "b.txt"), "你好世界")
    console.log("追加写入成功")
} catch (error) {
    console.log(error)
}

